package com.myacg.editor.common.unit.button;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JLabel;

import com.myacg.editor.common.unit.panel.base.DefaultPanelBar;
import com.myacg.editor.common.utils.FontUtils;

public class TextButton extends JLabel{
	private String buttonName ; 
	private double buttonNameSize;
	private Color  buttonNameColor;
	private Color  backgroundColor;
	private final Color  defaultBackBtColor;
	private final Color  defaultBtColor;
	public  TextButton(int width , int height , String  buttonName,Color buttonNameColor , Color backBtColor){
		 //宽高
		  this.setPreferredSize(new Dimension(width, height));
		  this.buttonName =buttonName;
		  this.buttonNameSize = 0.7 ;
		  this.buttonNameColor = buttonNameColor;
		  this.defaultBtColor = buttonNameColor;
		  this.defaultBackBtColor = backBtColor;
		  this.backgroundColor = backBtColor;
		  //注册一些基本的UI事件
		  defaultButtonEvent();
	}
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		  		  Graphics2D g2 = (Graphics2D) g; //
		  		RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(1, 1, getWidth()-2, getHeight()-2, 2,2);
	      //设置背景
		  g2.setColor(this.backgroundColor); 
		  g2.fill(roundedRectangle);
		  //设置按钮文字
	      g2.setColor(this.buttonNameColor); 
	      FontUtils.drawCenterStringByPercentage(buttonName,this.buttonNameSize,this, g2);
	}
    
	private  void defaultButtonEvent(){
		this.addMouseListener(new MouseAdapter() {
			Color enterColor = new Color(240, 0, 0);
			Color pressColor = new Color(200, 10, 10);
			@Override
			public void mouseEntered(MouseEvent e) {
				super.mouseEntered(e);
				 setButtonNameColor(Color.white);
				 setBackgroundColor(enterColor);
				 setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
			@Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				  setButtonNameColor( getDefaultBtColor());
				  setBackgroundColor( getDefaultBackBtColor());
				  setCursor(Cursor.getDefaultCursor());
			}
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				 setBackgroundColor(pressColor);
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				 setBackgroundColor(enterColor);
			}
		});
	}

	public String getButtonName() {
		return buttonName;
	}

	public void setButtonName(String buttonName) {
		this.buttonName = buttonName;
		this.repaint();
	}

	public double getButtonNameSize() {
		return buttonNameSize;
	}

	public void setButtonNameSize(double buttonNameSize) {
		this.buttonNameSize = buttonNameSize;
		this.repaint();
	}

	public Color getButtonNameColor() {
		return buttonNameColor;
	}

	public void setButtonNameColor(Color buttonNameColor) {
		this.buttonNameColor = buttonNameColor;
		this.repaint();
	}

	public Color getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
		this.repaint();
	}

	public Color getDefaultBtColor() {
		return defaultBtColor;
	}
	public Color getDefaultBackBtColor() {
		return defaultBackBtColor;
	}
	
}
